package com.NetWork.lesson1;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试IP类
//InetAddress 只有静态方法
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");//localhost
            System.out.println(inetAddress1);///127.0.0.1
            InetAddress inetAddress2 = InetAddress.getLocalHost();
            System.out.println(inetAddress2);//LAPTOP-EIUE8T7M/115.199.76.29

            //查询网站ip地址
            InetAddress inetAddress3 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress3);//www.baidu.com/180.101.49.12

            //常用方法
//            System.out.println(inetAddress3.getAddress());//[B@74a14482
//            System.out.println(inetAddress3.getCanonicalHostName());//;规范得名字180.101.49.12
            System.out.println(inetAddress3.getHostAddress());//ip 180.101.49.12
            System.out.println(inetAddress3 .getHostName());//域名www.baidu.com 或者自己电脑得名字


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

    